package bigbanksystemserver;

import java.io.IOException;
import javax.swing.DefaultComboBoxModel;

public class MainMenu extends javax.swing.JFrame {

    private BigBankSystemServerThread thread;
    private MainMenuController controller;

    public MainMenu() {
        controller = new MainMenuController();
        initComponents();
        selectbranch_combo.setModel(new DefaultComboBoxModel(controller.getBranchList()));
        setLocationRelativeTo(null);
        DraggableFrame frame = new DraggableFrame(this, panel);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainmenu = new javax.swing.JLayeredPane();
        stop_btn = new javax.swing.JButton();
        start_btn = new javax.swing.JButton();
        port_txt = new javax.swing.JTextField();
        port_label = new javax.swing.JLabel();
        selectbranch_combo = new javax.swing.JComboBox();
        selectbranch_label = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        outputconsole = new javax.swing.JTextArea();
        minimise_icon = new javax.swing.JButton();
        exit_icon = new javax.swing.JButton();
        title = new javax.swing.JLabel();
        logo = new javax.swing.JLabel();
        panel = new javax.swing.JLabel();
        background = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        setUndecorated(true);

        stop_btn.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        stop_btn.setText("Stop");
        stop_btn.setEnabled(false);
        stop_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                stop_btnActionPerformed(evt);
            }
        });
        stop_btn.setBounds(410, 65, 70, 30);
        mainmenu.add(stop_btn, javax.swing.JLayeredPane.DEFAULT_LAYER);

        start_btn.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        start_btn.setText("Start");
        start_btn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                start_btnActionPerformed(evt);
            }
        });
        start_btn.setBounds(335, 65, 70, 30);
        mainmenu.add(start_btn, javax.swing.JLayeredPane.DEFAULT_LAYER);

        port_txt.setEditable(false);
        port_txt.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        port_txt.setText("1001");
        port_txt.setBounds(275, 65, 50, 30);
        mainmenu.add(port_txt, javax.swing.JLayeredPane.DEFAULT_LAYER);

        port_label.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        port_label.setText("Port:");
        port_label.setBounds(235, 65, 40, 30);
        mainmenu.add(port_label, javax.swing.JLayeredPane.DEFAULT_LAYER);

        selectbranch_combo.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        selectbranch_combo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "B1", "B2", "B3", "B4", "B5" }));
        selectbranch_combo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                selectbranch_comboActionPerformed(evt);
            }
        });
        selectbranch_combo.setBounds(80, 65, 145, 30);
        mainmenu.add(selectbranch_combo, javax.swing.JLayeredPane.DEFAULT_LAYER);

        selectbranch_label.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        selectbranch_label.setText("Branch:");
        selectbranch_label.setBounds(20, 65, 60, 30);
        mainmenu.add(selectbranch_label, javax.swing.JLayeredPane.DEFAULT_LAYER);

        outputconsole.setColumns(20);
        outputconsole.setEditable(false);
        outputconsole.setLineWrap(true);
        outputconsole.setRows(5);
        outputconsole.setWrapStyleWord(true);
        jScrollPane1.setViewportView(outputconsole);

        jScrollPane1.setBounds(20, 130, 460, 245);
        mainmenu.add(jScrollPane1, javax.swing.JLayeredPane.DEFAULT_LAYER);

        minimise_icon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bigbanksystemserver/images/icon_minimise.png"))); // NOI18N
        minimise_icon.setBorderPainted(false);
        minimise_icon.setContentAreaFilled(false);
        minimise_icon.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        minimise_icon.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/bigbanksystemserver/images/icon_move_minimise.png"))); // NOI18N
        minimise_icon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                minimise_iconActionPerformed(evt);
            }
        });
        minimise_icon.setBounds(405, 0, 40, 26);
        mainmenu.add(minimise_icon, javax.swing.JLayeredPane.DEFAULT_LAYER);

        exit_icon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bigbanksystemserver/images/icon_exit.png"))); // NOI18N
        exit_icon.setBorderPainted(false);
        exit_icon.setContentAreaFilled(false);
        exit_icon.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        exit_icon.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/bigbanksystemserver/images/icon_move_exit.png"))); // NOI18N
        exit_icon.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exit_iconActionPerformed(evt);
            }
        });
        exit_icon.setBounds(445, 0, 50, 26);
        mainmenu.add(exit_icon, javax.swing.JLayeredPane.DEFAULT_LAYER);

        title.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        title.setForeground(new java.awt.Color(255, 255, 255));
        title.setText("BigBank Banking System Server");
        title.setBounds(138, 0, 224, 26);
        mainmenu.add(title, javax.swing.JLayeredPane.DEFAULT_LAYER);

        logo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bigbanksystemserver/images/logo_small.png"))); // NOI18N
        logo.setBounds(5, 5, 20, 20);
        mainmenu.add(logo, javax.swing.JLayeredPane.DEFAULT_LAYER);

        panel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bigbanksystemserver/images/servermainmenu.png"))); // NOI18N
        panel.setBounds(0, 0, 500, 30);
        mainmenu.add(panel, javax.swing.JLayeredPane.DEFAULT_LAYER);

        background.setIcon(new javax.swing.ImageIcon(getClass().getResource("/bigbanksystemserver/images/background.png"))); // NOI18N
        background.setBounds(0, 0, 500, 400);
        mainmenu.add(background, javax.swing.JLayeredPane.DEFAULT_LAYER);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainmenu, javax.swing.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainmenu, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void minimise_iconActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_minimise_iconActionPerformed
        setState(javax.swing.JFrame.ICONIFIED);
    }//GEN-LAST:event_minimise_iconActionPerformed

    private void exit_iconActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exit_iconActionPerformed
        ConfirmationDialog msg = new ConfirmationDialog(this, true, ConfirmationDialog.QUESTION_ICON, "Confirm Exit ?");

        if (msg.getSelectedOption() == msg.YES_OPTION) {
            System.exit(0);
        }
    }//GEN-LAST:event_exit_iconActionPerformed

    private void start_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_start_btnActionPerformed
        selectbranch_combo.setEnabled(false);
        start_btn.setEnabled(false);
        stop_btn.setEnabled(true);

        try {
            thread = new BigBankSystemServerThread(outputconsole, (String) selectbranch_combo.getSelectedItem(), Integer.parseInt(port_txt.getText()));
            thread.start();
        } catch (IOException ex) {
        }
    }//GEN-LAST:event_start_btnActionPerformed

    private void stop_btnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_stop_btnActionPerformed
        thread.stopServer();
        selectbranch_combo.setEnabled(true);
        start_btn.setEnabled(true);
        stop_btn.setEnabled(false);
    }//GEN-LAST:event_stop_btnActionPerformed

    private void selectbranch_comboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_selectbranch_comboActionPerformed
        port_txt.setText("100" + (selectbranch_combo.getSelectedIndex() + 1));
    }//GEN-LAST:event_selectbranch_comboActionPerformed

    public static void main(String args[]) {

        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainMenu().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel background;
    private javax.swing.JButton exit_icon;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel logo;
    private javax.swing.JLayeredPane mainmenu;
    private javax.swing.JButton minimise_icon;
    private javax.swing.JTextArea outputconsole;
    private javax.swing.JLabel panel;
    private javax.swing.JLabel port_label;
    private javax.swing.JTextField port_txt;
    private javax.swing.JComboBox selectbranch_combo;
    private javax.swing.JLabel selectbranch_label;
    private javax.swing.JButton start_btn;
    private javax.swing.JButton stop_btn;
    private javax.swing.JLabel title;
    // End of variables declaration//GEN-END:variables
}
